package com.enniswu.weitter.user.info;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.enniswu.weitter.R;
import com.enniswu.weitter.data.local.DBSupport;
import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.moment.postlist.PostListPresenter;

import java.util.ArrayList;
import java.util.List;

public class UserInfoPresenter extends PostListPresenter<UserInfoContract.View> implements UserInfoContract.Presenter {

    public UserInfoPresenter(UserInfoContract.View view, int id) {
        super(view, 0, id);
    }

    @Override
    public void getUser(int id) {
        uLoader.getInfo(id, new AbstractCallback<User>() {
            @Override
            public void onSuccess(@Nullable User user) {
                if (view == null)
                    return;
                if (user != null) {
                    view.updateInfo(user);
                }
            }
        });
    }

    @Override
    public void getFollowing() {
        uLoader.getFollowing(orientation, new AbstractCallback<List<Integer>>() {
            @Override
            public void onSuccess(@Nullable List<Integer> integers) {
                if (integers == null)
                    integers = new ArrayList<>(0);
                view.setFollowing(integers);
            }
        });
    }

    @Override
    public void getFollows() {
        uLoader.getFollows(orientation, new AbstractCallback<List<Integer>>() {
            @Override
            public void onSuccess(@Nullable List<Integer> integers) {
                if (integers == null)
                    integers = new ArrayList<>(0);
                view.setFollows(integers);
            }
        });
    }

    @Override
    public void follow() {
        if (handleNotLog())
            return;
        if (TextUtils.isEmpty(SPSupport.getInstance().getSessionId())) {
            view.showMessage(R.string.info_no_name);
            return;
        }
        uLoader.follow(orientation, new AbstractCallback<Object>() {
            @Override
            public void onSuccess(@Nullable Object o) {
                User user = DBSupport.getInstance().getCurrentUser();
                if (user != null) {
                    user.getFollowing().add(orientation);
                    DBSupport.getInstance().setCurrentUser(user);
                }
                view.setFollow(true);
            }
        });
    }

    @Override
    public void unfollow() {
        if (handleNotLog())
            return;
        uLoader.unfollow(orientation, new AbstractCallback<Object>() {
            @Override
            public void onSuccess(@Nullable Object o) {
                User user = DBSupport.getInstance().getCurrentUser();
                if (user != null) {
                    user.getFollowing().remove(orientation);
                    DBSupport.getInstance().setCurrentUser(user);
                }
                view.setFollow(false);
            }
        });
    }
}
